<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\enum\ConfigEnum;
use app\enum\KuaiShouEnum;
use app\service\fetch\KuaiShouFetchService;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;

class KsVideoDelStatusWork extends BaseWork
{
    public const REDIS_TASK_KEY = ConfigEnum::CACHE_CONFIG[KuaiShouEnum::LIST_CONFIG_KEY]['video_status_del'];

    protected function configure()
    {
        // 指令配置
        $this->setName('ks_video_del_status_work');
        $this->setDescription('更新快手删除视频状态');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $output->writeln('=======更新快手删除视频状态开始' . date('Y-m-d H:i:s', time()) . '=======');
        try {
            $redis = Cache::store('redis')->handler();
            $st = time() + 5 * 60;
            while (time() < $st) {
                $advertiserId = $redis->lPop(self::REDIS_TASK_KEY);
                if (! $advertiserId) {
                    usleep(500000);
                    continue;
                }
                $res = KuaiShouFetchService::fetchVideoDelStatus($advertiserId);
                if (! $res) {
                    $output->writeln('=======更新快手删除视频状态失败,账户id:' . $advertiserId . KuaiShouFetchService::getError(0) . date('Y-m-d H:i:s', time()) . '=======');
                }
            }
        } catch (\Exception $e) {
            $output->writeln('=======更新快手删除视频状态失败' . $e->getMessage() . date('Y-m-d H:i:s', time()) . '=======');
        }
        $output->writeln('=======更新快手删除视频状态结束' . date('Y-m-d H:i:s', time()) . '=======');
    }
}
